﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using PhoneAppSTS.Utils;
using Microsoft.Phone.Scheduler;

namespace PhoneAppSTS
{
    public partial class Settings : PhoneApplicationPage
    {
        public Settings()
        {
            ApplicationBar = App.AppBar;
            InitializeComponent();
            this.DataContext = App.Settings;
        }

        private void beginTimePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            if (e.NewDateTime != null)
            {
                App.Settings.BeginTimeSetting = e.NewDateTime;
                App.Settings.MiddleTimeSetting = e.NewDateTime.Value.AddHours(6);
                App.Settings.LastTimeSetting = e.NewDateTime.Value.AddHours(12);
            }

            if (App.Settings.BeginIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.MORNING));
                CreateNotification(Constants.MORNING);
            }

            if (App.Settings.MiddleIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.MIDDAY));
                CreateNotification(Constants.MIDDAY);
            }

            if (App.Settings.LastIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.EVENING));
                CreateNotification(Constants.EVENING);
            }
        }

        private void CreateNotification(int which)
        {
            String name = System.Guid.NewGuid().ToString();
            Reminder reminder = new Reminder(name);
            reminder.Title = "Przypomnienie o lekach!";
            reminder.Content = "Leki czas wziąć";
            DateTime dateTime = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0,0);

            if (which == Constants.MORNING)
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.BeginTimeSetting.Value.Hour, App.Settings.BeginTimeSetting.Value.Minute, App.Settings.BeginTimeSetting.Value.Second);
                App.Settings.BeginNotificationNameSetting = name;
            }
            else if (which == Constants.MIDDAY)
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.MiddleTimeSetting.Value.Hour, App.Settings.MiddleTimeSetting.Value.Minute, App.Settings.MiddleTimeSetting.Value.Second);
                App.Settings.MiddleNotificationNameSetting = name;
            }
            else
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.LastTimeSetting.Value.Hour, App.Settings.LastTimeSetting.Value.Minute, App.Settings.LastTimeSetting.Value.Second);
                App.Settings.LastNotificationNameSetting = name;
            }
            if (reminder.BeginTime < DateTime.Now)
            {
                reminder.BeginTime = reminder.BeginTime.AddDays(1);
            }
            DateTime exp = reminder.BeginTime.AddMinutes(5);
            reminder.ExpirationTime = exp;
            reminder.RecurrenceType = RecurrenceInterval.Daily;
            
            reminder.NavigationUri = new Uri("/ShowMedicines.xaml?which="+which, UriKind.Relative);

            // Register the reminder with the system.
            ScheduledActionService.Add(reminder);
        }

        private string DeleteNotification(int which)
        {
            if (which == Constants.MORNING)
            {
                return App.Settings.BeginNotificationNameSetting;
            }
            else if (which == Constants.MIDDAY)
            {
                return App.Settings.MiddleNotificationNameSetting;
            }
            else
            {
                return App.Settings.LastNotificationNameSetting;
            }
        }

        private void beginCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.BeginIsCheckedSetting = true;
            CreateNotification(Constants.MORNING);
            
        }

        private void beginCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.BeginIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.MORNING));
        }

        private void middleCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.MiddleIsCheckedSetting = true;
            CreateNotification(Constants.MIDDAY);
        }

        private void middleCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.MiddleIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.MIDDAY));
        }

        private void lastCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.LastIsCheckedSetting = true;
            CreateNotification(Constants.EVENING);
        }

        private void lastCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.LastIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.EVENING));
        }
    }
}